package rssreaderapplication;

import edu.app.dao.IDAO;
import edu.app.dao.RecetteDAO;
import edu.app.gui.Menu;
import edu.app.persistence.Recette;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class EventsFrame extends javax.swing.JFrame {
byte[] person_image = null;
    String url;
 private IDAO<Recette> recetteDAO= new RecetteDAO();
 private String ch2;
 private String ch;

    /**
     * Creates new form EventsRSSFrame
     */
    public EventsFrame(String url) {
        this.url = url;
        initComponents();
        setResizable(false);
        setBounds(50, 50,1200, 670);
        getContentPane().setLayout(null);
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();
        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2)-20;
        this.setLocation(windowX, windowY);
         TableCellRenderer rendererFromHeader = jTable1.getTableHeader().getDefaultRenderer();
        JLabel headerLabel = (JLabel) rendererFromHeader;
        headerLabel.setHorizontalAlignment(JLabel.CENTER);
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(SwingConstants.CENTER);
        jTable1.getColumn("Recettes").setCellRenderer(dtcr);
        //jTable1.getColumn("Auteur").setCellRenderer(dtcr);
        jTable1.getColumn("Auteur").setCellRenderer(dtcr);
       // jTable1.getColumn("Date").setCellRenderer(dtcr);
         jTable1.getColumnModel().getColumn(2).setMinWidth(0);
         jTable1.getColumnModel().getColumn(2).setMaxWidth(0);
         jTable1.getColumnModel().getColumn(3).setMinWidth(0);
         jTable1.getColumnModel().getColumn(3).setMaxWidth(0);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        getContentPane().setLayout(null);

        jTable1.setFont(new java.awt.Font("Gabriola", 0, 24));
        jTable1.setModel(new EventTableModel(url)
        );
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTable1.setRowHeight(45);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(20, 150, 370, 450);

        jLabel1.setFont(new java.awt.Font("Gabriola", 3, 36));
        jLabel1.setForeground(new java.awt.Color(0, 102, 255));
        jLabel1.setText("Recettes publieés par d'autres utilisateurs");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(330, 0, 550, 62);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/add1.jpg"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel3MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel3MouseExited(evt);
            }
        });
        getContentPane().add(jLabel3);
        jLabel3.setBounds(840, 120, 80, 70);

        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Gabriola", 0, 24));
        jTextArea1.setRows(5);
        jTextArea1.setAutoscrolls(false);
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(400, 150, 370, 440);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/send.jpg"))); // NOI18N
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel4MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel4MouseExited(evt);
            }
        });
        getContentPane().add(jLabel4);
        jLabel4.setBounds(980, 120, 110, 70);

        jLabel5.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel5.setText("Ajouter a mes recettes");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(810, 190, 130, 30);

        jLabel6.setFont(new java.awt.Font("Gabriola", 3, 36));
        jLabel6.setForeground(new java.awt.Color(51, 102, 255));
        jLabel6.setText("Description");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(480, 100, 210, 40);

        jLabel7.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel7.setText("Contacter l'auteur");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(970, 190, 110, 31);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/print1.jpg"))); // NOI18N
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel8MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel8MouseExited(evt);
            }
        });
        getContentPane().add(jLabel8);
        jLabel8.setBounds(1110, 120, 70, 70);

        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret1.jpg"))); // NOI18N
        jLabel16.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel16MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel16MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel16MouseExited(evt);
            }
        });
        getContentPane().add(jLabel16);
        jLabel16.setBounds(30, 10, 90, 50);

        jLabel14.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel14.setText("Precedent");
        getContentPane().add(jLabel14);
        jLabel14.setBounds(30, 60, 80, 31);

        jLabel9.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel9.setText("Imprimer");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(1120, 190, 70, 31);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(310, 52, 590, 10);

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/cons.jpg"))); // NOI18N
        jLabel10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel10MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel10MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel10MouseExited(evt);
            }
        });
        getContentPane().add(jLabel10);
        jLabel10.setBounds(960, 250, 70, 64);

        jLabel11.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel11.setText("Consulter dans Happyfood.com");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(900, 310, 180, 31);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/rss.jpg"))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(0, -150, 1200, 790);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
     ch=jTable1.getValueAt(jTable1.getSelectedRow(),2).toString();
         ch.replace("i","x");
         ch2=ch.replace("-", " \n ");
         ch=ch2.replace(".", " \n ");
         ch2=ch.replace(",", " \n ");
         ch=ch2.replace(";", " \n ");
         ch2=ch.replace(":", " \n ");
         ch=ch2.replace("[", " \n ");
         ch2=ch.replace("]", " \n ");
         jTextArea1.setText(ch2);
         
       // jTextArea1.setText(jTable1.getValueAt(jTable1.getSelectedRow(),2).toString());
    }//GEN-LAST:event_jTable1MouseClicked

    private void jLabel3MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseEntered
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/add2.jpg")));
    }//GEN-LAST:event_jLabel3MouseEntered

    private void jLabel3MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseExited
       jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/add1.jpg")));
    }//GEN-LAST:event_jLabel3MouseExited

    private void jLabel4MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseEntered
      jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/send2.jpg")));
    }//GEN-LAST:event_jLabel4MouseEntered

    private void jLabel4MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseExited
             jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/send.jpg")));

    }//GEN-LAST:event_jLabel4MouseExited

    private void jLabel8MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseEntered
              jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/print2.jpg")));

    }//GEN-LAST:event_jLabel8MouseEntered

    private void jLabel8MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseExited
                      jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/print1.jpg")));

    }//GEN-LAST:event_jLabel8MouseExited

    private void jLabel10MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseEntered
                     jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/cons2.jpg")));

    }//GEN-LAST:event_jLabel10MouseEntered

    private void jLabel10MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseExited
                  jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/cons.jpg")));

    }//GEN-LAST:event_jLabel10MouseExited

    private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel10MouseClicked
        Desktop d=Desktop.getDesktop();
        try {
            try {
                // Browse a URL, say google.com
                d.browse(new URI(jTable1.getValueAt(jTable1.getSelectedRow(),3).toString()));
            } catch (URISyntaxException ex) {
                Logger.getLogger(EventsFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(EventsFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jLabel10MouseClicked

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        if (recetteDAO.find(jTable1.getValueAt(jTable1.getSelectedRow(),0).toString())==null){
        Recette r = new Recette(Recette.idinc,jTable1.getValueAt(jTable1.getSelectedRow(),0).toString(),"",jTable1.getValueAt(jTable1.getSelectedRow(),2).toString(),1,1,1,"",person_image,1,1,"");
        recetteDAO.create(r);
        JOptionPane.showMessageDialog(this,"Recette ajoutée avec succés", "Ok", JOptionPane.INFORMATION_MESSAGE);}
        else
            JOptionPane.showMessageDialog(this,"Recette dejas existante", "Ok", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jLabel3MouseClicked

    private void jLabel16MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseClicked

        this.setVisible(false);
        
}//GEN-LAST:event_jLabel16MouseClicked

    private void jLabel16MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseEntered
        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret2.jpg")));
}//GEN-LAST:event_jLabel16MouseEntered

    private void jLabel16MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseExited
        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret1.jpg")));
}//GEN-LAST:event_jLabel16MouseExited

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel.
         */

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EventsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EventsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EventsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EventsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EventsFrame("http://happyfood.net84.net/?feed=rss2").setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}
